package hhm.utils.client;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Upload {
	public static void main(String[] args) {

		CloseableHttpClient httpClient = HttpClients.createDefault();

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope("192.168.10.31", 80), new UsernamePasswordCredentials("admin", ""));

		HttpHost target = new HttpHost("192.168.10.31", 80, "http");
		HttpClientContext context = HttpClientContext.create();

		context.setCredentialsProvider(credsProvider);

		HttpPost httpPost = new HttpPost("/config/icfg_conf_upload");
		try {

			File file = new File("E:\\hhm_utils\\trunk\\src\\main\\resources\\6211");
			MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create()
					.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

			mEntityBuilder.addPart("file_name", new StringBody("running-config", ContentType.MULTIPART_FORM_DATA));
			mEntityBuilder.addPart("merge", new StringBody("false", ContentType.MULTIPART_FORM_DATA));

			mEntityBuilder.addBinaryBody("source_file", file, ContentType.APPLICATION_OCTET_STREAM, "running-config");

			mEntityBuilder.addPart("file_name_radio",
					new StringBody("running-config", ContentType.MULTIPART_FORM_DATA));
			mEntityBuilder.addPart("merge_mode_radio",
					new StringBody("merge_mode_radio_r", ContentType.MULTIPART_FORM_DATA));
			httpPost.setEntity(mEntityBuilder.build());
			HttpResponse response = httpClient.execute(target, httpPost, context);
			String result = EntityUtils.toString(response.getEntity());

			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
